---
layout: "default"
title: "zip"
description: "Swift documentation for 'zip': Creates a sequence of pairs built out of two underlying sequences."
keywords: "zip,func,swift,documentation"
root: "/v4.2"
---

<div class="declaration" id="func-zip-sequence1-sequence2_-sequence1_-sequence2">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-zip-sequence1-sequence2_-sequence1_-sequence2">func zip&lt;Sequence1, Sequence2&gt;(<wbr>_:<wbr> Sequence1, _: Sequence2)</a>
        
<div class="comment collapse" id="comment-func-zip-sequence1-sequence2_-sequence1_-sequence2"><div class="p">
    <p>Creates a sequence of pairs built out of two underlying sequences.</p>

<p>In the <code>Zip2Sequence</code> instance returned by this function, the elements of
the <em>i</em>th pair are the <em>i</em>th elements of each underlying sequence. The
following example uses the <code>zip(_:_:)</code> function to iterate over an array
of strings and a countable range at the same time:</p>

<pre><code class="language-swift">let words = [&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;]
let numbers = 1...4

for (word, number) in zip(words, numbers) {
    print(&quot;\(word): \(number)&quot;)
}
// Prints &quot;one: 1&quot;
// Prints &quot;two: 2
// Prints &quot;three: 3&quot;
// Prints &quot;four: 4&quot;</code></pre>

<p>If the two sequences passed to <code>zip(_:_:)</code> are different lengths, the
resulting sequence is the same length as the shorter sequence. In this
example, the resulting array is the same length as <code>words</code>:</p>

<pre><code class="language-swift">let naturalNumbers = 1...Int.max
let zipped = Array(zip(words, naturalNumbers))
// zipped == [(&quot;one&quot;, 1), (&quot;two&quot;, 2), (&quot;three&quot;, 3), (&quot;four&quot;, 4)]</code></pre>

<p><strong>Parameters:</strong>
  <strong>sequence1:</strong> The first sequence or collection to zip.
  <strong>sequence2:</strong> The second sequence or collection to zip.
<strong>Returns:</strong> A sequence of tuple pairs, where the elements of each pair are
  corresponding elements of <code>sequence1</code> and <code>sequence2</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func zip&lt;Sequence1, Sequence2&gt;(_ sequence1: Sequence1, _ sequence2: Sequence2) -&gt; Zip2Sequence&lt;Sequence1, Sequence2&gt; where Sequence1 : Sequence, Sequence2 : Sequence</code>
    
    
</div></div>
</div>
